﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace Baubixinh_client
{
    public partial class frmAddProduct : DevExpress.XtraEditors.XtraForm
    {
        public bool PRODUCTADDED { get; set; }
        private DataTable mobjManufacturer;

        public frmAddProduct()
        {
            InitializeComponent();
        }

        private void frmAddProduct_Load(object sender, EventArgs e)
        {
            try
            {
                mobjManufacturer = clsDbAccess.fncGetManufacturer(Program.gobjDb);
                cbNhaSX.Properties.DataSource = mobjManufacturer;

                treeDanhMuc.DataSource = Program.gobjDataDanhMuc;
                treeDanhMuc.ExpandAll();
                txtName.Focus();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (!objValidation.Validate()) return;
                
                List<DevExpress.XtraTreeList.Nodes.TreeListNode> lstCheckedNode;
                lstCheckedNode = treeDanhMuc.GetAllCheckedNodes();
                if (lstCheckedNode.Count <= 0)
                {
                    clsCommon.fncMsgError("Bạn chưa chọn danh mục sản phẩm.");
                    return;
                }

                if (clsDbAccess.fncIsDuplicateEAN(Program.gobjDb, txtEAN.Text.Trim()))
                {
                    clsCommon.fncMsgError("Mã sản phẩm đã tồn tại, hãy nhập mã khác.");
                    txtEAN.Focus();
                    return;
                }

                if (!clsCommon.fncMsgConfirm("Bạn có chắc muốn thêm sản phẩm mới?")) return;

                clsDbAccess.stProduct stData = new clsDbAccess.stProduct();
                stData.strName = txtName.Text.Trim();
                stData.strEAN = txtEAN.Text.Trim();
                stData.intSo_luong = clsCommon.fncCnvNullToInt(txtQty.Text.Trim());
                stData.intNhaSX = clsCommon.fncCnvNullToInt(cbNhaSX.EditValue);

                int intProID;
                int intCatID;
                int intGiaGoc;
                int intGiaLe;
                int intGiaCap1;
                int intGiaCap2;
                int intGiaCap3;
                bool blnSuccess = true;

                Cursor.Current = Cursors.WaitCursor;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                //insert san pham
                blnSuccess = blnSuccess && clsDbAccess.fncInsertProduct(Program.gobjDb, Program.gobjTrans, stData);

                //lay id san pham vua them
                intProID = clsDbAccess.fncGetMaxProduct(Program.gobjDb);

                //them gia ban
                intGiaGoc = clsCommon.fncCnvNullToInt(txtGiaGoc.Text.Trim());
                intGiaLe = clsCommon.fncCnvNullToInt(txtGiaLe.Text.Trim());
                intGiaCap1 = clsCommon.fncCnvNullToInt(txtGiaCap1.Text.Trim());
                intGiaCap2 = clsCommon.fncCnvNullToInt(txtGiaCap2.Text.Trim());
                intGiaCap3 = clsCommon.fncCnvNullToInt(txtGiaCap3.Text.Trim());
                blnSuccess = blnSuccess && clsDbAccess.fncInsertGiaBan(Program.gobjDb, Program.gobjTrans, intProID, intGiaGoc, intGiaLe, intGiaCap1, intGiaCap2, intGiaCap3);

                for (int i = 0; i <= lstCheckedNode.Count - 1; i++)
                {
                    if (!blnSuccess) break;
                    //them sp vao danh muc
                    intCatID = clsCommon.fncCnvNullToInt(lstCheckedNode[i].GetValue(colCategory_id_danhmuc));
                    blnSuccess = blnSuccess && clsDbAccess.fncInsertProduct_Category(Program.gobjDb, Program.gobjTrans, intProID, intCatID);
                }

                if (blnSuccess)
                {
                    Program.gobjTrans.Commit();
                    clsCommon.fncMsgInfo("Thêm mới thành công.");
                    this.Close();
                }
                else
                {
                    clsCommon.fncMsgError("Thêm mới sản phẩm thất bại. Xin hãy kiểm tra lại dữ liệu.");
                    Program.gobjTrans.Rollback();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }
    }
}